package com.example.locationreport.locationreport.http;


import android.text.TextUtils;

import com.example.locationreport.locationreport.bean.CommonBean;
import com.example.locationreport.locationreport.bean.UpdateBean;
import com.example.locationreport.locationreport.bean.VehicleInfoBean;
import com.example.locationreport.locationreport.bean.log.BaseConfigBean;
import com.example.locationreport.locationreport.bean.log.CommandLogBean;
import com.example.locationreport.locationreport.bean.log.CommandLogBean.CommandBean;
import com.example.locationreport.locationreport.bean.log.LoginBean;
import com.example.locationreport.locationreport.global.Constant;
import com.example.locationreport.locationreport.global.ConstantInfo;
import com.example.locationreport.locationreport.http.network.OkGoBackListener;
import com.example.locationreport.locationreport.http.network.OkGoCallBackListener;
import com.example.locationreport.locationreport.utils.CommonUtils;
import com.example.locationreport.locationreport.utils.JsonUtils;
import com.example.locationreport.locationreport.utils.LogLocationUtils;
import com.example.locationreport.locationreport.utils.SharedPreUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

/**
 * @author : YanKing
 * @date : 2021/12/11  11:49
 * @Description : 网络连接
 */
public class NetworkUtils {

    /**
     * Gis定位终端
     */

    // 根据imei号获取车辆信息接口地址
    public static void getVehicleInfoByImei(final OkGoBackListener listener) {

        OkGo.<String>get(listener.getUrl())
                .tag(listener.getUrl())
                .params(Constant.IMEI , CommonUtils.getImeiID())
                .execute(new OkGoCallBackListener(listener) {
                    @Override
                    public void onSuccess(String body) {
                        if(JsonUtils.parseJsonToBean(body, CommonBean.class).getData() != null)
                            listener.onSuccess(JsonUtils.parseJsonToBean(JsonUtils.parseJsonToBean(body, CommonBean.class).getData().toString() , VehicleInfoBean.class));
                    }
                });
    }

    // 获取App更新信息
    public static void getUpdateAppInfo(final OkGoBackListener listener) {

        OkGo.<String>get(listener.getUrl())
                .tag(listener.getUrl())
                .execute(new OkGoCallBackListener(listener) {
                    @Override
                    public void onSuccess(String body) {
                        listener.onSuccess(JsonUtils.parseJsonToBean(body , UpdateBean.class));
                    }
                });
    }

    /**
     * 日志回传
     */
    // 登录
    public static void login(OkGoBackListener listener){
        OkGo.<String>post(Url.Login)
                .tag(listener.getUrl())
                .upJson(ConstantInfo.LOGIN_VALUE)
                .execute(new OkGoCallBackListener(listener) {
                    @Override
                    public void onSuccess(String body) {
                        LoginBean loginBean = JsonUtils.parseJsonToBean(body, LoginBean.class);

                        ConstantInfo.TOKEN = loginBean.token ;

                        listener.onSuccess(loginBean);
                    }
                });
    }

    // 根据设备ID获取设备配置
    public static void getConfigByID(final OkGoBackListener listener) {
        OkGo.<String>get(Url.get_Config_By_ID + CommonUtils.getImeiID())
                .tag(listener.getUrl())
                .headers(Constant.TOKEN , ConstantInfo.TOKEN)
                .execute(new OkGoCallBackListener(listener) {
                    @Override
                    public void onSuccess(String body) {
                        listener.onSuccess(JsonUtils.parseJsonToBean(body, BaseConfigBean.class).getData());
                    }
                });
    }

    // 上传本地配置
    public static void saveConfigByID(final OkGoBackListener listener ,  String configJson) {
        String time = SharedPreUtils.getString(Constant.SP_CONFIG_TIME);

        JSONObject json = new JSONObject();
        try {
            json.put(Constant.UpdateTime , TextUtils.isEmpty(time) ? CommonUtils.getTimeStr() : time);
            json.put(Constant.DeviceID , CommonUtils.getImeiID());
            json.put(Constant.ConfigJSON , new JSONObject(configJson).toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        OkGo.<String>post(Url.save_Config_By_ID)
                .tag(listener.getUrl())
                .headers(Constant.TOKEN , ConstantInfo.TOKEN)
                .upJson(json)
                .execute(new OkGoCallBackListener(listener) {
                    @Override
                    public void onSuccess(String body) {
                        SharedPreUtils.putString(Constant.SP_CONFIG_TIME , CommonUtils.getTimeStr());
                        listener.onSuccess(JsonUtils.parseJsonToBean(body , CommonBean.class));
                    }
                });
    }

    // 根据设备ID获取是否有日志上传需求
    public static void getCommandByID(final OkGoBackListener listener) {
        OkGo.<String>post(Url.get_Command_By_ID)
                .tag(listener.getUrl())
                .headers(Constant.TOKEN , ConstantInfo.TOKEN)
                .params(Constant.DeviceID , CommonUtils.getImeiID())
                .execute(new OkGoCallBackListener(listener) {
                    @Override
                    public void onSuccess(String body) {
                        listener.onSuccess(JsonUtils.parseJsonToBean(body , CommandLogBean.class).getData());
                    }
                });
    }

    /**
     * 上传日志文档
     */
    public static void uploadCommandFile(final OkGoBackListener listener , CommandBean commandBean) {
        String urlValue = "?" ;
        urlValue += Constant.UploadDeviceID + "=" + CommonUtils.getImeiID() ;
        urlValue += "&" + Constant.CommandID + "=" + commandBean.getId() ;
        urlValue += "&" + Constant.FileName + "=" + commandBean.getStartTime() + ".zip";

        LogLocationUtils.writeLog(Constant.NetworkLogFileName , "上传文档 :" + urlValue);

        ConstantInfo.isUpload = true ;
        File uploadFile = new File(ConstantInfo.ZIP_PATH);

        OkGo.<String>post(Url.upload_Command_File + urlValue)
                .tag(listener.getUrl())
                .headers(Constant.TOKEN, ConstantInfo.TOKEN)
                .params(Constant.File, uploadFile)
                .execute(new OkGoCallBackListener(listener) {
                    @Override
                    public void onSuccess(String body) {
                        ConstantInfo.isUpload = false ;

                        listener.onSuccess(JsonUtils.parseJsonToBean(body , CommonBean.class));

                        uploadFile.delete();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                        ConstantInfo.isUpload = false ;

                        uploadFile.delete();
                    }
                });
    }
}